package com.techysky.service.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.techysky.entity.SpareHistory;
import com.techysky.entity.SpareManage;
import com.techysky.repository.SpareHistoryRepository;
import com.techysky.repository.SpareRepository;
import com.techysky.service.DataService;
import com.techysky.util.DBUtil;
import com.techysky.vo.DataBase;


@Service("dataService")
public class DataServiceImpl implements DataService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private transient SpareRepository spareRepository;
	@Autowired
	private transient SpareHistoryRepository spareHistoryRepository;
	
	@Override
	public String synPad(String ip,String ids) {
		
		DataBase db=new DataBase();
		db.setIp(ip);
		
		if(ids.contains("synBj")){
			synBj(db);
		}
		return "success";
	}

	public void synBj(DataBase db) {
		
		JdbcTemplate template=DBUtil.jdbcTemplate(db);
		sm(template);
		sh(template);
	}
//SpareManage
	private void sm(JdbcTemplate template) {
		List<SpareManage> smlist=template.query("select id,spare_Code,spare_Name,spare_No,spare_Quantity,spare_Supplier,spare_Type from spare_manage ", (ResultSet rs, int rowNum)->{
				SpareManage spareManage=new SpareManage();
				spareManage.setId(rs.getString("id"));
				spareManage.setSpareCode(rs.getString("spare_Code"));
				spareManage.setSpareName(rs.getString("spare_Name"));
				spareManage.setSpareNo(rs.getString("spare_No"));
				spareManage.setSpareQuantity(rs.getString("spare_Quantity"));
				spareManage.setSpareSupplier(rs.getString("spare_Supplier"));
				spareManage.setSpareType(rs.getString("spare_Type"));
				return spareManage;
		});
		
	
		List<SpareManage> spareManagelist=spareRepository.findAll();
		int num=spareManagelist.size();
		int num2=smlist.size();
		
		if(num>num2){
			template.execute("delete from spare_manage");
			template.batchUpdate("INSERT INTO spare_manage (id,spare_Code,spare_Name,spare_No,spare_Quantity,spare_Supplier,spare_Type) VALUES (?,?,?,?,?,?,?)", new BatchPreparedStatementSetter(){

				@Override
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					ps.setString(1, spareManagelist.get(i).getId());
					ps.setString(2, spareManagelist.get(i).getSpareCode());
					ps.setString(3, spareManagelist.get(i).getSpareName());
					ps.setString(4, spareManagelist.get(i).getSpareNo());
					ps.setString(5, spareManagelist.get(i).getSpareQuantity());
					ps.setString(6, spareManagelist.get(i).getSpareSupplier());
					ps.setString(7, spareManagelist.get(i).getSpareType());
					
				}

				@Override
				public int getBatchSize() {
					return spareManagelist.size();
				}
				
			});
		}else if(num<num2){
			spareRepository.deleteAllInBatch();
			spareRepository.save(smlist);
		}
	}
   //SpareHistory
	private void sh(JdbcTemplate template) {
		List<SpareHistory> shlist=template.query("select id,spare_Code,spare_Name,spare_No,spare_Quantity,spare_Supplier,spare_Type from spare_history ", (ResultSet rs, int rowNum)->{
			SpareHistory spareHistory=new SpareHistory();
			spareHistory.setId(rs.getString("id"));
			spareHistory.setSpareCode(rs.getString("spare_Code"));
			spareHistory.setSpareName(rs.getString("spare_Name"));
			spareHistory.setSpareNo(rs.getString("spare_No"));
			spareHistory.setSpareQuantity(rs.getString("spare_Quantity"));
			spareHistory.setSpareSupplier(rs.getString("spare_Supplier"));
			spareHistory.setSpareType(rs.getString("spare_Type"));
			return spareHistory;
	     });
		List<SpareHistory> spareHistorylist=spareHistoryRepository.findAll();
		int n1=shlist.size();
		int n2=spareHistorylist.size();
		if(n1>n2){
			spareHistoryRepository.deleteAllInBatch();
			spareHistoryRepository.save(shlist);
		}else if(n1<n2){
			template.execute("delete from spare_history");
			template.batchUpdate("INSERT INTO spare_history (id,spare_Code,spare_Name,spare_No,spare_Quantity,spare_Supplier,spare_Type) VALUES (?,?,?,?,?,?,?)", new BatchPreparedStatementSetter(){

				@Override
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					ps.setString(1, spareHistorylist.get(i).getId());
					ps.setString(2, spareHistorylist.get(i).getSpareCode());
					ps.setString(3, spareHistorylist.get(i).getSpareName());
					ps.setString(4, spareHistorylist.get(i).getSpareNo());
					ps.setString(5, spareHistorylist.get(i).getSpareQuantity());
					ps.setString(6, spareHistorylist.get(i).getSpareSupplier());
					ps.setString(7, spareHistorylist.get(i).getSpareType());
				}

				@Override
				public int getBatchSize() {
					return spareHistorylist.size();
				}
				
			});
		}
	}
	@Override
	public Object backComputer(String ip, String path) {
		return null;
	}
	/**
	 * //备份摆件数据
	public Object backBj(Connection conn,String path) {
		return null;
	}
	//同步备件
	
	//备份手工录入
	public Object backSg(Connection conn,String path) {
		return null;
	}
	//同步手工录入
	public Object synSg(Connection conn) {
		return null;
	}
	//备份状态监控
	public Object backZt(Connection conn,String path) {
		return null;
	}
	//同步状态监控
	public Object synZt(Connection conn,String path) {
		return null;
	}
	//备份电子履历
	public Object backLl(Connection conn,String path) {
		return null;
	}
	//同步电子履历
	public Object synLl(Connection conn) {
		return null;
	}*/
}
